﻿using System;
using System.Data;
using System.Data.SqlServerCe;
using System.Linq;
using System.Collections.Generic;
using Home.Mobilesales.Common;
using Home.MobileSales.Common.Entities;


namespace Home.MobileSales.Common.Models
{
    public class BuyPointRepository
    {
        public List<BuyPoint> GetBuyPointsByBuyer(int idBuyer)
        {
            List<BuyPoint> buyPoints = new List<BuyPoint>();
            try
            {
                using (SqlCeConnection connection = WorkDataBase.CreateConnection())
                {
                    if (connection.State != ConnectionState.Open)
                        connection.Open();
                    string sqlQuery = "SELECT idBuyPoint, NameBuyPoint, Adress , Debts " +
                        "from BuyPoints "+
                        "where BuyerId=@BuyerId";
                    using (SqlCeCommand command = new SqlCeCommand(sqlQuery, connection))
                    {
                        command.Parameters.Add("@BuyerId", idBuyer);
                        using (SqlCeDataReader dr = command.ExecuteReader())
                        {
                            int idBuyPoint = dr.GetOrdinal("idBuyPoint");
                            int nameBuyPoint = dr.GetOrdinal("NameBuyPoint");
                            int adressBuyPoint = dr.GetOrdinal("Adress");
                            int debts = dr.GetOrdinal("Debts");
                            BuyPoint buyPoint = null;
                            while (dr.Read())
                            {
                                buyPoint = new BuyPoint();
                                buyPoint.IdBuyPoint = dr.GetInt32(idBuyPoint);
                                buyPoint.NameBuyPoint = dr.IsDBNull(nameBuyPoint)
                                                            ? string.Empty
                                                            : dr.GetString(nameBuyPoint);
                                buyPoint.Adress = dr.IsDBNull(adressBuyPoint)
                                                      ? string.Empty
                                                      : dr.GetString(adressBuyPoint);
                                buyPoint.Debts = dr.IsDBNull(debts) ? 0.00f : dr.GetFloat(debts);
                                buyPoints.Add(buyPoint);
                            }
                            return buyPoints;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public BuyPoint GetBuyPointById(int id)
        {
            using (SqlCeConnection connection = WorkDataBase.CreateConnection())
            {

                if (connection.State != ConnectionState.Open)
                    connection.Open();
                string sqlQuery =
                    "SELECT IdBuyPoint, NameBuyPoint, Adress, BuyerId, Debts "+
                    "FROM BuyPoints "+
                    "where IdBuyPoint = @IdBuyPoint";
                using (SqlCeCommand com = new SqlCeCommand(sqlQuery, connection))
                {
                    com.Parameters.Add("@IdBuyPoint", id);
                    using (SqlCeDataReader dr = com.ExecuteReader())
                    {
                        BuyPoint buyPoint = new BuyPoint();
                        int idBuyPoint = dr.GetOrdinal("IdBuyPoint");
                        int nameBuyPoint = dr.GetOrdinal("NameBuyPoint");
                        int adress = dr.GetOrdinal("Adress");
                        int debts = dr.GetOrdinal("Debts");
                        int buyerId = dr.GetOrdinal("BuyerId");
                        if (dr.Read())
                        {
                            buyPoint.IdBuyPoint = dr.GetInt32(idBuyPoint);
                            buyPoint.NameBuyPoint = dr.GetString(nameBuyPoint);
                            buyPoint.Adress = dr.IsDBNull(adress) ? string.Empty : dr.GetString(adress);
                            buyPoint.Debts = dr.IsDBNull(debts) ? 0.00f : dr.GetDouble(debts);
                            buyPoint.Buyer = dr.IsDBNull(buyerId)
                                                 ? null
                                                 : DataManager.GetInstance().Buyers.GetBuyerById(dr.GetInt32(buyerId));
                        }
                        return buyPoint;
                    }
                }
            }
        }
    }
}
